import { RuleObject, StoreValue } from 'ant-design-vue/lib/form/interface';

const validate = {
  // 电话号码
  phone: async (_: RuleObject, value: StoreValue) => {
    if (!value) {
      return Promise.reject('值不能为空');
    }
    const re =
      /^((0\d{2,3}-\d{7,8})|((19[0-9])|(16[0-9])|(13[0-9])|(14[5,7,9])|(15[^4])|(18[0-9])|(17[0,1,3,5,6,7,8]))\d{8})$/g;
    if (re.test(value)) {
      return Promise.resolve();
    } else {
      return Promise.reject('必须为合法电话');
    }
  },
  // 数字
  number: async (_: RuleObject, value: StoreValue) => {
    const re = new RegExp('^[0-9]*$');
    if (re.test(value)) {
      return Promise.resolve();
    } else {
      return Promise.reject('必须为数字');
    }
  },
  // 检验长度
  len16: async (_: RuleObject, value: StoreValue) => {
    const re = /^\d{16}$/g;
    if (re.test(value)) {
      return Promise.resolve();
    } else {
      return Promise.reject('长度必须为16');
    }
  },
};

export default validate;
